﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UDP_Library;

namespace WinFormsDemo
{
    public partial class FormDemo : Form
    {
        OmegaUDP omegaUDP = new OmegaUDP();

        public FormDemo()
        {
            InitializeComponent();
        }

        private void FormDemo_Load(object sender, EventArgs e)
        {
            omegaUDP.Got_Log += OmegaUDP_Got_Log;
            //准备这里载入配置文件，先不写。最后补充。
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            //https://blog.csdn.net/TheBestAge/article/details/85331742
        }
        /// <summary>
        /// 日志 数据回传
        /// </summary>
        /// <param name="type">[Log][New Data][Send Data][Error]</param>
        /// <param name="log"></param>
        /// <param name="data"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void OmegaUDP_Got_Log(string type, string log, byte[] data)
        {
            switch (type)
            {
                case "Log":
                    {
                        if (checkBox_Log.Checked)
                            WriteLog(type, log);
                    }
                    break;
                case "New Data":
                    {
                        if (checkBox_NewData.Checked)
                            WriteLog(type, log, data);

                        if (checkBox_AutoSend.Checked)
                        {
                            //收到数据自动转发
                            string str = JieXiBytes(data);
                            textBox_Data.Text = str;
                            button_SendDataToRemote.PerformClick();
                        }
                    }
                    break;
                case "Send Data":
                    {
                        if (checkBox_SendData.Checked)
                            WriteLog(type, log, data);
                    }
                    break;
                case "Error":
                    {
                        if (checkBox_Error.Checked)
                            WriteLog(type, log);
                    }
                    break;
                default:
                    {
                        WriteLog(type, log);
                    }
                    break;
            }
        }

        private void WriteLog(string type, string log,byte[] data = null)
        {
            DateTime dt = DateTime.Now;
            string BuildMessage = $"{dt}\t【{type}】\t{log}";
            if (data != null)
            {
                //如果传递进来数据，就把数据也显示出来。
                #region 数据制作
                string str = JieXiBytes(data);

                BuildMessage = $"{BuildMessage} [{str}]";
                #endregion
            }

            if (textBox_Log.Text.Trim() == "")
            {
                textBox_Log.Text = BuildMessage;
            }
            else
            {
                textBox_Log.Text = $"{textBox_Log.Text}\r\n{BuildMessage}";
            }
            this.textBox_Log.Focus();//获取焦点
            this.textBox_Log.Select(this.textBox_Log.TextLength, 0);//光标定位到文本最后
            this.textBox_Log.ScrollToCaret();//滚动到光标处

        }



        #region 软件主要按钮功能

        /// <summary>
        /// 启动监听（这里应该有启动和停止监听功能）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_StartLintener_Click(object sender, EventArgs e)
        {
            omegaUDP.Set_Local(textBox_LocalAddress.Text, int.Parse(textBox_LocalPort.Text));
            if (omegaUDP.IsListening)
            {
                omegaUDP.Stop_Listener_Local();
                button_StartLintener.Text = "开始监听本地端口";
                button_StartLintener.BackColor = Color.LimeGreen;
            }
            else
            {
                omegaUDP.Start_Listener_Local();
                button_StartLintener.Text = "停止监听本地端口";
                button_StartLintener.BackColor = Color.Red;                
            }
            
        }
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_SendDataToRemote_Click(object sender, EventArgs e)
        {
            //制作数据
            byte[] data = MakeBytes();

            //发送数据
            omegaUDP.SendData_To_Remote(textBox_RemoteAddress.Text,int.Parse(textBox_RemotePort.Text), data);
        }

        private void button_ClearLog_Click(object sender, EventArgs e)
        {
            textBox_Log.Text = "";
        }
        #endregion

        private byte[] MakeBytes()
        {
            byte[] data = null;
            if (radioButton_UTF8.Checked)
            {
                data = UDP_Library.DataConvert.string2bytes_UTF8(textBox_Data.Text);
            }
            if (radioButton_ASCII.Checked)
            {
                data = UDP_Library.DataConvert.string2bytes_ASCII(textBox_Data.Text);
            }
            if (radioButton_RealData.Checked)
            {
                data = UDP_Library.DataConvert.string2bytes_Default(textBox_Data.Text);
            }

            return data;
        }
        private string JieXiBytes(byte[] data)
        {
            string str = "";
            if (radioButton_UTF8.Checked)
            {
                str = UDP_Library.DataConvert.bytes2string_UTF8(data);
            }
            if (radioButton_ASCII.Checked)
            {
                str = UDP_Library.DataConvert.bytes2string_ASCII(data);
            }
            if (radioButton_RealData.Checked)
            {
                str = UDP_Library.DataConvert.bytes2string_Default(data);
            }
            return str;
        }


    }
}
